<?php
//商品类别标签管理
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;

class PagController extends CommonController
{
    public function index()
    {
       //
    }
	//商品类别标签添加操作界面
    public function create()
    {
		$data = \DB::table("m_genre")->get();
		$gid = $_GET['gid'];
		return view('admin.pag.add',['list'=>$data,'gid'=>$gid]);
    }
	//商品类别标签后台添加
    public function store(Request $request)
    {
		$data = $request->all();
		$data = $request->except('_token');
		$n = \DB::table("m_pag")->where('name',$data['name'])->where('gid',$data['gid'])->first();
		//判断		
		if(empty($n)){
			$id = \DB::table("m_pag")->insertGetId($data);
			$info = "商品类别标签添加成功！";
		}else{
			$info = "添加失败,本类别中标签此名称存在无法添加";
		}
		return redirect()->action('admin\PagController@show',[$data['gid']])->with("err",$info);
    }
	 //同类商品标签浏览
    public function show($id)
    {
		$data = \DB::table("m_pag")->where('gid',$id)->first();
		//若没有此类别的标签 调用add方法
		if(empty($data)){
			$id = $this->add($id);
		}
		$data = \DB::table("m_pag")->where('gid',$id)->paginate(5);
		return view("admin.pag.show",['list'=>$data]);
    }
	//添加一个对应类别下的方法
	public function add($id)
	{
		$n = \DB::table("m_pag")->insertGetId(['gid'=>$id]);
		return $id;
	}
	//商品类别标签修改操作界面
    public function edit($id)
    {
		$data = \DB::table("m_pag")->where('id',$id)->first();
		return view("admin.pag.edit",['list'=>$data]);
    }
	//商品类别标签后台修改
    public function update(Request $request, $id)
    {
		$data =	$request->all();
		$data = $request->except('_token','_method');
		\DB::table('m_pag')->where('id',$id)->update($data);
		$info = "商品类别标签修改成功！";
		return redirect()->action('admin\PagController@show',[$data['gid']])->with("err",$info);
    }
	//商品类别标签后台删除
    public function destroy($id)
    {
		\DB::table('m_pag')->where('id', $id)->delete();
		$info = "商品类别标签删除成功！";
		return back()->with("err",$info);
        //return view("admin.pic.info",['info'=>$info]);
    }
}
